import axios from 'axios'

const state = {
  servers: [],
  currentServer: null
}

const mutations = {
  SET_SERVERS(state, servers) {
    state.servers = servers
  },
  SET_CURRENT_SERVER(state, server) {
    state.currentServer = server
  },
  ADD_SERVER(state, server) {
    state.servers.push(server)
  },
  UPDATE_SERVER(state, updatedServer) {
    const index = state.servers.findIndex(s => s.id === updatedServer.id)
    if (index !== -1) {
      state.servers.splice(index, 1, updatedServer)
    }
  },
  REMOVE_SERVER(state, serverId) {
    state.servers = state.servers.filter(s => s.id !== serverId)
  }
}

const actions = {
  async fetchServers({ commit }) {
    try {
      const response = await axios.get('/api/servers')
      commit('SET_SERVERS', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async createServer({ commit }, serverData) {
    try {
      const response = await axios.post('/api/servers', serverData)
      commit('ADD_SERVER', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async updateServer({ commit }, { id, serverData }) {
    try {
      const response = await axios.put(`/api/servers/${id}`, serverData)
      commit('UPDATE_SERVER', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async deleteServer({ commit }, id) {
    try {
      await axios.delete(`/api/servers/${id}`)
      commit('REMOVE_SERVER', id)
      return true
    } catch (error) {
      throw error
    }
  },
  
  // 获取Linux服务器磁盘信息
  async getLinuxDiskInfo({ commit }, serverId) {
    try {
      const response = await axios.get(`/api/servers/${serverId}/disk-info/linux`)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  // 获取Windows服务器磁盘信息
  async getWindowsDiskInfo({ commit }, serverId) {
    try {
      const response = await axios.get(`/api/servers/${serverId}/disk-info/windows`)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  // 获取Linux服务器目录结构
  async getLinuxDirectoryStructure({ commit }, { serverId, path = '/' }) {
    try {
      const response = await axios.get(`/api/servers/${serverId}/directory-structure/linux?path=${encodeURIComponent(path)}`)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  // 获取Windows服务器目录结构
  async getWindowsDirectoryStructure({ commit }, { serverId, path = 'C:\\' }) {
    try {
      const response = await axios.get(`/api/servers/${serverId}/directory-structure/windows?path=${encodeURIComponent(path)}`)
      return response.data
    } catch (error) {
      throw error
    }
  }
}

const getters = {
  allServers: state => state.servers,
  currentServer: state => state.currentServer,
  serversByOsType: (state) => (osType) => {
    return state.servers.filter(server => server.osType === osType)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}